//
//  SHOriginalSectionView.swift
//  SHWeibo
//
//  Created by jingming wang on 16/7/28.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit
import SnapKit

class SHOriginalSectionView: UIView {
    
    // 记录当前原创微博的底部约束
    var orginalViewBottomConstraint:Constraint?
    
    // MARK: - 懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    // 昵称
    private lazy var nameLabel: UILabel = UILabel(text: "", font: 15)
    // 微博等级
    private lazy var rankImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    // 微博时间
    private lazy var timeLabel: UILabel = UILabel(text: "", textColor: K_THEME_COLOR, font: 10)
    // 微博来源
    private lazy var sourceLabel: UILabel = UILabel(text: "", textColor: K_THEME_COLOR, font: 10)
    // 认证logo
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    // 微博内容
    private lazy var contentLabel: UILabel = UILabel(text: "", textColor: UIColor.darkGrayColor(), font: 15, numberOfLines: 0, maxWidth: K_SCREEN_WIDTH-20)
    // 配图
    private lazy var pictureView: SHPictureView = SHPictureView()
    
    //// MARK: - 定义一个ViewModel供外界调用
    var userInfoViewModel: SHUserInfoViewModel?{
        didSet{
            
            // 设置头像
            headImageView.sh_setImage(userInfoViewModel?.data?.userInfo?.profile_image_url, placeholderImgName: "avatar_default")
            // 昵称
            nameLabel.text = userInfoViewModel?.data?.userInfo?.name
            // 时间
            timeLabel.text = userInfoViewModel?.sinaTimeStr
            //来源
            sourceLabel.attributedText = userInfoViewModel?.sourceAttr
            // 会员等级
            rankImageView.image = userInfoViewModel?.rankImage
            // 认证用户
            avatarImageView.image = userInfoViewModel?.avatarImage
            // 微博正文
            contentLabel.attributedText = userInfoViewModel?.originalContentAttr
            
            // 配图
            
            /*
            - 首先要卸载
            - 如果count > 0 代表有配图
            - 01 赋值
            - 02 更改约束
            - 03 显示配图（cell复用问题）
            - 反之 代表没有配图
            - 01 更改约束
            - 02 隐藏配图（cell复用问题）
            */
            // 卸载约束
            orginalViewBottomConstraint?.uninstall()
            
            // 代表有配图
            if userInfoViewModel?.data?.pic_urls?.count > 0{
                // 赋值
                pictureView.pictuerModels = userInfoViewModel?.data?.pic_urls
                // 更改约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.orginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(homeCellMargin).constraint
                })
                // 显示配图
                pictureView.hidden = false
            }else {
                // 代表没有配图
                // 更改约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.orginalViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(homeCellMargin).constraint
                })
                // 隐藏配图
                pictureView.hidden = true
            }

        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        backgroundColor = UIColor.whiteColor()
        
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(rankImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        // 添加约束
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.leading.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView)
            make.leading.equalTo(headImageView.snp_trailing).offset(10)
        }
        rankImageView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        avatarImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.leading.equalTo(headImageView)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(homeCellMargin)
            make.leading.equalTo((K_SCREEN_WIDTH - (cellWH*3 + cellMargin*2))/2)
        }
        // 给当前原创微博设置约束
        self.snp_makeConstraints { (make) -> Void in
            self.orginalViewBottomConstraint = make.bottom.equalTo(pictureView).offset(homeCellMargin).constraint
        }
    }
}

extension SHOriginalSectionView {


}